<?php

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 */

class design_banner extends MX_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('Admin_Layout');
        $this->load->model('banner/banner_model');
        
    }
    
    public function index()
    {
        $this->mycommon->authAdmin();
        $data = array();
        $data['headTitle'] = 'Quản lý banner';
        $data['current'] = 12;
        $data['banner'] = $this->banner_model->getBannerOntion();
        $this->admin_layout->render('design_banner/design_banner_view', $data);
    }
    
    public function add()
    {
        $this->mycommon->authAdmin();
        $data = array();
        $data['error'] = '';
        $this->load->library('Form_validation');
        $this->form_validation->CI =& $this;
        $this->form_validation->set_error_delimiters('<li>','</li>');
        $data['current'] = 4;
//        $data['parentCategory'] = $this->category_model->getParentCategory();
        if ($this->input->post('submit') && $this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('name_network','Tên nhóm', 'required|min_length[3]');
//            $this->form_validation->set_rules('name_en', 'Tên chỉ mục(tiếng anh)', 'required|min_length[3]');
//            if ($this->input->post('check_href') == '1') {
//                $this->form_validation->set_rules('hrefUrl', 'Đường dẫn bên thứ ba', 'required|callback_checkUrl');
//            }
            $this->form_validation->set_message('required','%s không được để trống');
            $this->form_validation->set_message('min_length','%s ít nhất %d ký tự');
//            $this->form_validation->set_message('checkUrl', '%s không chính xác');
            if ($this->form_validation->run() == TRUE) {
//                $parent_id = $this->input->post('parentid');
                $name = $this->input->post('name_network');
//                $name_en = $this->input->post('name_en');
//                $href = $this->input->post('hrefUrl');
//                var_dump($this->group_model->groupExists($name));exit;
                if (! $this->network_model->networkExists($name)) {
                    if ($this->network_model->addNameNetwork($name)) {
                        redirect('admin/network/index');
                    }
                }
            }
        }
        $data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->admin_layout->render('network/add_network_view', $data);
    }
    
 public function hide($Id = '')
    {
        $this->mycommon->authAdmin();
        if ($Id == '') {
            show_404();
        }
        $data = array(
            'delete_flag' => '1'
        );
        $this->network_model->updateNameNetworkDeleteF($Id, $data);
        redirect('admin/network/index');
    }
    
    public function show($Id = '')
    {
        $this->mycommon->authAdmin();
        if ($Id == '' && intval($Id) != 0) {
            show_404();
        }
        $Id = intval($Id);
        $data = array(
            'delete_flag' => '0'
        );
        $this->network_model->updateNameNetworkDeleteF($Id, $data);
        redirect('admin/network/index');
    }
    
    public function delete($Id = '')
    {
        $this->mycommon->authAdmin();
        if ($Id == '' && intval($Id) != 0) {
            show_404();
        }
        $Id = intval($Id);
//        $this->load->model('setting/setting_model');
        $this->network_model->deleteNameNetwork($Id);
//        foreach($arrMenubar as $k=>$v) {
//            if ($v == $catId) {
//                unset($arrMenubar[$k]);
//                $arrMenubar = array_values($arrMenubar);
//                break;
//            }
//        }
//        foreach($arrBoxHome as $_k=>$_v) {
//            if ($_v == $catId) {
//                unset($arrBoxHome[$_k]);
//                $arrBoxHome = array_values($arrBoxHome);
//                break;
//            }
//        }
//        $lstMenubar = implode(',', $arrMenubar);
//        $lstBoxHome = implode(',', $arrBoxHome);
//        $this->setting_model->setOption('lstMenubar', $lstMenubar);
//        $this->setting_model->setOption('lstBoxHome', $lstBoxHome);
        redirect('admin/group/index');
    }
    
    public function edit($Id = '')
    {
        $this->mycommon->authAdmin();
        if ($Id == '' && intval($Id) != 0) {
            show_404();
        }
        $Id = intval($Id);
        $data = array();
        $data['error'] = '';
        $this->load->library('Form_validation');
        $this->form_validation->CI =& $this;
        $this->form_validation->set_error_delimiters('<li>','</li>');
        $data['current'] = 4;
        if ($this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('name_vn','Tên chỉ mục(tiếng việt)', 'required|min_length[3]');
            $this->form_validation->set_rules('name_en', 'Tên chỉ mục(tiếng anh)', 'required|min_length[3]');
            $this->form_validation->set_message('required','%s không được để trống');
            $this->form_validation->set_message('min_length','%s ít nhất %d ký tự');
            $this->form_validation->set_message('checkUrl', '%s không chính xác');
            if ($this->form_validation->run() == TRUE) {
                $name_vn = $this->input->post('name_vn');
                $name_en = $this->input->post('name_en');
//                $href = $this->input->post('hrefUrl');
                $array = array(
//                    'name_vn'   =>  $name_vn,
                    'name'   =>  $name_en,
//                    'href'      =>  $href
                );
//                debug($this->group_model->updateNameGroup($Id, $array));
                if ($this->network_model->updateNameNetwork($Id, $array)) {
                    redirect('admin/network/index');
                }
            }
        }

        $tmp = $this->network_model->getAllNetwork();
        $data['categoryInfo'] = $tmp[0];
        $data['headTitle'] = 'Sửa tên nhà mạng';
        $data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->admin_layout->render('network/network_edit', $data);
    }
    
    public function updateNetworkPrice() {
        $this->mycommon->authAdmin();
        $data = array();
        $data['error'] = '';
        $this->load->library('Form_validation');
        $this->form_validation->CI =& $this;
        $this->form_validation->set_error_delimiters('<li>','</li>');
        $sim = $this->sim_model->getAllSim();
        $data['current'] = 4;
        if ($this->input->post('frmToken') && $this->session->userdata('frmToken') && $this->input->post('frmToken') == $this->session->userdata('frmToken')) {
            $this->form_validation->set_rules('name_network',' Tên nhóm sim', 'required');
            $this->form_validation->set_rules('type_group',' Loại hình thức', 'required');
            $this->form_validation->set_rules('price_network_new', 'Giá nhóm sim mới', 'required|min_length[3]');
            $this->form_validation->set_message('required','%s không được để trống');
            $this->form_validation->set_message('min_length','%s ít nhất %d ký tự');
            if ($this->form_validation->run() == TRUE) {
                $name_network = $this->input->post('name_network');
                $price_network_new = $this->input->post('price_network_new');
                $price_network_new = str_replace(',', '', $price_network_new);
                $price_network_new = intval($price_network_new);
                $typePirce = $this->input->post('type_group');
                $price_network_new = ($typePirce == 2) ? (0 - $price_network_new) : $price_network_new;
//                $type_group = $this->input->post('type_group');
                $list_network_id = intval($name_network);
                if($this->network_model->checkNetworkExists($list_network_id)&& $price_network_new != 0){
                    if ($this->network_model->updatePriceNetwork('list_network_id', $list_network_id, $price_network_new)) {
                        redirect('admin/variation/index');
                    }
                }
            }
//            debug('end');
        }

        $data['tmp'] = $this->network_model->getAllNetwork();
        $data['headTitle'] = 'Sửa giá đại lý sim';
        $data['frmToken'] = uniqid();
        $this->session->set_userdata('frmToken', $data['frmToken']);
        $this->admin_layout->render('network/update_price_network_view', $data);
    }
}

?>
